package com.xbb.controller.user;

import com.xbb.common.RespResult;
import com.xbb.constant.SessionConstant;
import com.xbb.entity.User;
import com.xbb.mapper.UserMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    private String prefix = "/user/user/";

    @Resource
    private UserMapper userMapper;

    // 编辑个人资料
    @RequestMapping("/personal.html")
    public String personalHtml () {
        return prefix + "personal";
    }

    // 重置密码
    @RequestMapping("/password.html")
    public String passwordHtml () {
        return prefix + "password";
    }

    @ResponseBody
    @RequestMapping("/personal/edit")
    public RespResult editPersonal(@RequestBody User user, HttpSession session) {
        userMapper.updateByPrimaryKeySelective(user);
        // 更新 session
        User u = userMapper.selectByPrimaryKey(user.getId());
        session.removeAttribute(SessionConstant.KEY_USER);
        session.setAttribute(SessionConstant.KEY_USER, u);
        return new RespResult();
    }

}

